package com.ts.hidden;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.ts.hidden.base.AbsFragmentActivity;
import com.ts.hidden.fragment.login.ConfirmFragment;
import com.ts.hidden.fragment.login.CreateFragment;
import com.ts.hidden.fragment.login.LoginFragment;
import com.ts.hidden.fragment.login.IntroduceFragment;
import com.ts.hidden.fragment.login.RestoreFragment;
import com.ts.hidden.fragment.login.ConfirmFragment.IConfirmListener;
import com.ts.hidden.fragment.login.CreateFragment.ICreateListener;
import com.ts.hidden.fragment.login.LoginFragment.ILoginListener;
import com.ts.hidden.fragment.login.IntroduceFragment.IMainListener;
import com.ts.hidden.fragment.login.RestoreFragment.IRestoreListener;
import com.ts.tube.utils.PreferenceUtil;

public class HiddenMainActivity extends AbsFragmentActivity implements IMainListener, ICreateListener,
        IConfirmListener, ILoginListener, IRestoreListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hidden_main);
    }

    @Override
    protected Fragment onCreateMainFragment(Bundle savedInstanceState) {
        if (PreferenceUtil.getPreference(this, MsConst.KEY_PASSWORD, "default").equals("default")
                || PreferenceUtil.getPreference(this, MsConst.KEY_EMAIL, "default").equals("default")) {
            PreferenceUtil.clearPreferences(this);
            return IntroduceFragment.newInstance("");
        } else {
            return LoginFragment.newInstance("");
        }
    }

    @Override
    protected int getFragmentContainerId() {
        return R.id.ll_main_login;
    }

    @Override
    public void onIMainNext() {
        CreateFragment createFragment = CreateFragment.newInstance("");
        showFragment(createFragment, true);

    }

    @Override
    public void onICreateNext(String currentPw) {
        ConfirmFragment confirmFragment = ConfirmFragment.newInstance(currentPw);
        showFragment(confirmFragment, true);

    }

    @Override
    public void onIConfirmOK(String currentPw, String prePassword) {
        if (currentPw.equals(prePassword)) {
            Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show();
            PreferenceUtil.setPreference(this, MsConst.KEY_PASSWORD, currentPw);

            RestoreFragment restoreFragment = RestoreFragment.newInstance(false);
            showFragment(restoreFragment, true);
        } else {
            Toast.makeText(this, "Not Match", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onILogin() {
        gotoMediaActivity();
    }

    private void gotoMediaActivity() {
        startActivity(new Intent(this, MediaActivity.class));
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if(!PreferenceUtil.getPreference(this, MsConst.KEY_PASSWORD, "default").equals("default")) {
            MenuInflater menuInflater = getMenuInflater();
            menuInflater.inflate(R.menu.hidden_main, menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // PreferenceUtil.clearPreferences(this);
        switch (item.getItemId()) {
        case R.id.action_settings:
            RestoreFragment restoreFragment = RestoreFragment.newInstance(true);
            showFragment(restoreFragment, true);
            break;

        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onIRestoreNext() {
        gotoMediaActivity();
    }

    @Override
    public void onIRestore() {
        PreferenceUtil.clearPreferences(this);
        Toast.makeText(this, "Clear password successful !!!", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this, HiddenMainActivity.class));
        finish();

    }
}
